/******************************************************************************
 *                  Shanghai ChipON Micro-Electronic Co.,Ltd                  *
 ******************************************************************************
 *  $File Name$       : main.c                                         	      *
 *  $Author$          : ChipON AE/FAE Group                                   *
 *  $Data$            : 2021-07-8                                             *
 *  $AutoSAR Version  : V1.0	                                              *
 *  $Description$     : This is a KF32A156 device StdPeriph Template		  *
 ******************************************************************************
 *  Copyright (C) by Shanghai ChipON Micro-Electronic Co.,Ltd                 *
 *  All rights reserved.                                                      *
 *                                                                            *
 *  This software is copyrght protected and proprietary to                    *
 *  Shanghai ChipON Micro-Electronic Co.,Ltd.                                 *
 ******************************************************************************
 *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  *
 *                     		REVISON HISTORY                               	  *
 *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  *
 *  Data       Version  Author        Description                             *
 *  ~~~~~~~~~~ ~~~~~~~~ ~~~~~~~~~~~~  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  *
 *  2021-07-08 00.01.00 FAE Group     new creat                               *
 *                                                                            *
 *                                                                            *
 *****************************************************************************/

/******************************************************************************
*                        		Include Files                                 *
******************************************************************************/
#include "system_init.h"
#include "Canfd.h"
static void Led_Gpio_Init(void);
static void Led_Flip(void);


static void Led_Gpio_Init(void)
{
/* 
CCP0CH1    PD12
CCP21CH3   PH3
ECCP5CH3H  PA4
EPWM13A    PF7
 */
	GPIO_Write_Mode_Bits(GPIOA_SFR, GPIO_PIN_MASK_4 , GPIO_MODE_OUT);
	GPIO_Write_Mode_Bits(GPIOD_SFR, GPIO_PIN_MASK_12 , GPIO_MODE_OUT);
	GPIO_Write_Mode_Bits(GPIOF_SFR, GPIO_PIN_MASK_7 , GPIO_MODE_OUT);
	GPIO_Write_Mode_Bits(GPIOH_SFR, GPIO_PIN_MASK_3 , GPIO_MODE_OUT);

}


/**
 *  @brief :
 *  @param in :None
 *  @param out :None
 *  @retval :None
 */
static void Led_Flip(void)
{
	GPIO_Toggle_Output_Data_Config(GPIOA_SFR, GPIO_PIN_MASK_4);
	GPIO_Toggle_Output_Data_Config(GPIOD_SFR, GPIO_PIN_MASK_12);
	GPIO_Toggle_Output_Data_Config(GPIOF_SFR, GPIO_PIN_MASK_7);
	GPIO_Toggle_Output_Data_Config(GPIOH_SFR, GPIO_PIN_MASK_3);
}


/*
 * 1M  BAUDRATE
 * */
int main()
{
	Can_Controller_InterruptFlagType Can_Controller_InterruptFlag;
	uint8_t Rmc_count = 0;
	/* Initialize the system clock */
	SystemInit(120);
	/* Setup SysTick Timer as delay function */
	systick_delay_init(120);
	Canfd_Gpio_Init();
	Led_Gpio_Init();
	Can_m_FdControllerDeInit(1);
	Can_m_FdControllerInit(1, Canfd_Controller_AllConfig,0);
	Canfd_Sdu.Can_MailboxHeader[0].TransceiveType = MAIL_RECEIVE;
	Can_m_Fd_Write(1, &Canfd_Sdu);
	while (1)
	{
		Can_m_FdGetIntFlag(1, &Can_Controller_InterruptFlag, &Rmc_count);
		if(Can_Controller_InterruptFlag.RxIntFlag == 0x01)
		{
			Can_m_FdClearIntFlag(1, &Can_Controller_InterruptFlag, Rmc_count);
			Can_m_Fd_Read(1, &Canfd_Sdu);
			for(uint8_t rx_count = 0;rx_count<Rmc_count;rx_count++)
			{
				Canfd_Sdu.Can_MailboxHeader[rx_count].TransceiveType = MAIL_TRANSMIT;
			}
			Canfd_Sdu.Sdu_Length = Rmc_count;
			Can_m_Fd_Write(1, &Canfd_Sdu);
			Can_m_FdTransmitpack(1);
			for(uint8_t rx_count = 0;rx_count<Canfd_Sdu.Sdu_Length;rx_count++)
			{
				Canfd_Sdu.Can_MailboxHeader[rx_count].TransceiveType = MAIL_RECEIVE;
			}	
			Can_m_Fd_Write(1, &Canfd_Sdu);
		}
		Led_Flip();
	}
}
